// 重写数组 1. 获取原来数组方法
let oldArrayProtoMethods = Array.prototype
// 2.继承
export let ArrayMethods = Object.create(oldArrayProtoMethods)
// 3.劫持
let methods = ['push', 'pop', 'unshift', 'shift', 'splice']
// 遍历这些方法
methods.forEach((item) => {
  ArrayMethods[item] = function (...args) {
    // args改变后传入的值
    let result = oldArrayProtoMethods[item].apply(this, args)
    // 对数组添加对象形式进行劫持
    let inserted
    switch (item) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.splice(2) // arr.splice(索引,个数,追加的参数)
        break
    }
    let ob = this.__ob__
    if (inserted) ob.observerArray(inserted) // 对新增的每一项进行观测
    return result
  }
})
